package cn.lzy.service.impl;

import cn.lzy.dao.CommentMapper;
import cn.lzy.dao.StatisticMapper;
import cn.lzy.model.domain.Comment;
import cn.lzy.model.domain.Statistic;
import cn.lzy.service.ICommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Name: lanyuting
 * @CreateTime: 2023-10-31  16:58
 */

@Service
@Transactional
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private StatisticMapper statisticMapper;


    // 根据文章id分页查询评论
    @Override
    public PageInfo<Comment> getComments(Integer aid, int page, int count) {
        PageHelper.startPage(page, count);
        List<Comment> commentList = commentMapper.selectCommentWithPage(aid);
        PageInfo<Comment> commentInfo = new PageInfo<>(commentList);
        return commentInfo;
    }

    // 用户发表评论
    @Override
    public void pushComment(Comment comment) {
        commentMapper.pushComment(comment);
        // 更新文章评论数据量
        Statistic statistic = statisticMapper.selectStatisticWithArticleId(comment.getArticleId());
        statistic.setCommentsNum(statistic.getCommentsNum() + 1);
        statisticMapper.updateArticleCommentsWithId(statistic);

    }

    @Override
    public void deleteCommentById(Integer id) {
        commentMapper.deleteCommentById(id);
    }

    @Override
    public void updateCommentById(Integer aid, Integer id, String content) {
        commentMapper.updateCommentById(aid, id, content);
    }

    //后台评论删除
    public void deleteCommentById(int id) {
        commentMapper.deleteCommentById(id);
    }

    //后台评论更新
    public void updateCommentById(int aid, int id, String content) {
        commentMapper.updateCommentById(aid, id, content);
    }


}

